<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class budget extends Controller
{
	private  $_template_f; 
	
	function __construct()
	{
		parent::Controller();
		$permitid = $this->session->userdata('permitid');
		if(!$this->session->userdata('uname') || !is_numeric($permitid) || $permitid > -1)
		{
			redirect(site_url('login/'));
			die();
		}
		$this->_lang = $this->mycommon->getLangCode();	
		$this->_template_f = $this->config->item('template_f');
		$this->load->language('budget/budget', $this->_lang);
		$this->load->helper('breadcrumb');
        $this->load->model('budget/budget_model');
	}
	
	function index()
	{
		$data = array();
		$data['lang'] = $this->lang->line('budget');
		//Get param
		$uid = -1;
		$groupid = $this->input->get('gid', true);
		if($groupid == '') $groupid = -1;
		$groupid = (int)$groupid;
		$permitid = -1;//(int)$this->session->userdata('permitid');
		$typetime = (int)$this->input->get('type', true);
		$startdate = trim($this->input->get('fd', true));
		$enddate = trim($this->input->get('td', true));
		if($startdate == '' || $enddate == '')
		{
			$startdate = new DateTime();
			$startdate->sub(date_interval_create_from_date_string('7 days'));
			$startdate = $startdate->format('Y-m-d');
			$enddate = date('Y-m-d');
		}

		$page = (int)$this->input->get('page', true);
		if($page < 1) $page = 1;
		//->
		$report = $this->budget_model->get_report_financial($uid,$permitid,$groupid,$startdate,$enddate,$typetime,$page);
		$data['items'] = $report->items;
		$data['pages'] = $report->pages->totalpage;
		$data['page'] = $page;
		$data['groupid'] = $groupid;
		$data['startdate'] = $startdate;
		$data['enddate'] = $enddate;
		$data['typetime'] = $typetime;
		$data['summary'] = $report->summary;
		$data['groups'] = $this->budget_model->get_group_list();
		
		$this->_loadHeader();
		$this->_loadMenu();
		$this->load->view($this->_template_f . '/budget/index', $data);
		$this->_loadFooter();
	}
    function export()
    {
        
		$data = array();
		$data['lang'] = $this->lang->line('budget');
		//Get param
		$uid = -1;
		$groupid = $this->input->get('gid', true);
		if($groupid == '') $groupid = -1;
		$groupid = (int)$groupid;
		$permitid = -1;
		$typetime = (int)$this->input->get('type', true);
		$startdate = trim($this->input->get('fd', true));
		$enddate = trim($this->input->get('td', true));
		if($startdate == '' || $enddate == '')
		{
			$startdate = new DateTime();
			$startdate->sub(date_interval_create_from_date_string('7 days'));
			$startdate = $startdate->format('Y-m-d');
			$enddate = date('Y-m-d');
		}
		$page = (int)$this->input->get('page', true);
		if($page < 1) $page = 1;
		$report = $this->budget_model->get_report_financial($uid,$permitid,$groupid,$startdate,$enddate,$typetime,$page);
		$data['items'] = $report->items;
		$data['pages'] = $report->pages->totalpage;
		$data['page'] = $page;
		$data['groupid'] = $groupid;
		$data['startdate'] = $startdate;
		$data['enddate'] = $enddate;
		$data['typetime'] = $typetime;
		$data['summary'] = $report->summary;
		$data['groups'] = $this->budget_model->get_group_list();
    	
    	// ten dai ly
		foreach ($data['groups'] as $item){
			if($item['groupid']==$data['groupid']){
				$data['get_group_name'] = $item['groupname'];
			}
		}
		if(!isset($data['get_group_name'])){
			$data['get_group_name']='Tất cả';
		}
		
		//-> loai thoi gian
		if($typetime == 1){
			$data['t_time'] = 'Tuần';
		}else if($typetime == 2){
			$data['t_time'] = 'Tháng';
		}else if($typetime == 3){
			$data['t_time'] = 'Năm';
		}else{
			$data['t_time'] = 'Ngày';
		}
		
		
		$data['type_export'] = $type = (int)$this->input->get('type_export');
        if($data['type_export'] != 0){
        	
            $this->load->view($this->_template_f . '/budget/export_excel', $data, true);
        }
        else
        {
            $this->load->library('pdfreport');
            $html = $this->load->view($this->_template_f . '/budget/export_pdf', $data, true);
            $this->load->library('myvietstring');
            $pdf_name = 'admin_bugdet_admarket_' . $this->session->userdata('uname') . '_' .$startdate.'_'.$enddate .
                '_' . date('d.m.Y') . '.pdf';
            $this->pdfreport->html2pdf($html, $pdf_name);
        }
    }
	function agency()
	{
		$data = array();
		$data['lang'] = $this->lang->line('budget');
		//Get param
		$groupid = (int)$this->input->get('gid', true);
		$typetime = (int)$this->input->get('type', true);
		$startdate = trim($this->input->get('fd', true));
		$enddate = trim($this->input->get('td', true));
		$usr = trim($this->input->get('usr',true));
		if($startdate == '' || $enddate == '')
		{
			$startdate = new DateTime();
			$startdate->sub(date_interval_create_from_date_string('7 days'));
			$startdate = $startdate->format('Y-m-d');
			$enddate = date('Y-m-d');
		}
		$keyword = trim($this->input->get('keyword'), true);

		$page = (int)$this->input->get('page', true);
		if($page < 1) $page = 1;
		
		$limit = (int)$this->input->get('limit',true);
		if($limit == 0) $limit = 20;
		//->
		$report = $this->budget_model->get_report_financial_agency($groupid,$startdate,$enddate,$typetime,$usr,$page,$limit);
		$data['items'] = $report->items;
		$data['pages'] = $report->pages->totalpage;
		$data['page'] = $page;
		$data['limit'] = $limit;
		$data['groupid'] = $groupid;
		$data['startdate'] = $startdate;
		$data['enddate'] = $enddate;
		$data['typetime'] = $typetime;
		$data['usr'] = $usr;
		$data['keyword'] = $keyword;
		$data['summary'] = $report->summary;
		$data['groups'] = $this->budget_model->get_group_list();
		$data['groupname'] = 'default';
		foreach ($data['groups'] as $item)
		{
			if($item['groupid'] == $groupid)
			{
				$data['groupname'] = $item['groupname'];
				break;
			}
		}

		$this->_loadHeader();
		$this->_loadMenu();
		$this->load->view($this->_template_f . '/budget/agency', $data);
		$this->_loadFooter();
	}
	
	function user()
	{
		$type = trim($this->input->get('type', true));
		if($type == '') $type = -1;
		$type = (int)$type;
		
		$status = trim($this->input->get('status', true));
		if($status == '') $status = -1;
		$status = (int)$status;
		
		$page = (int)$this->input->get('page', true);
		if($page < 1) $page = 1;
		
		$startdate = trim($this->input->get('fd', true));
		$enddate = trim($this->input->get('td', true));
		if($startdate == '' || $enddate == '')
		{
			$startdate = new DateTime();
			$startdate->sub(date_interval_create_from_date_string('7 days'));
			$startdate = $startdate->format('Y-m-d');
			$enddate = date('Y-m-d');
		}
		
		$limit = (int)$this->input->get('limit',true);
		if($limit == 0) $limit = 20;
		
		$data = array();
		$data['groupid'] = (int)$this->input->get('gid', true);
		$data['uname'] = trim($this->input->get('u', true));
		$data['uid'] = (int)$this->input->get('uid', true);
		
		$logs = $this->budget_model->get_payment_logs($data['uid'], -1, $startdate, $enddate, $type, $status, $page, $limit);
		
		$data['info'] = $logs->info;
		$data['items'] = $logs->items;
		$data['lang'] = $this->lang->line('budget');
		$data['page'] = $page;
		$data['totalPage'] = $logs->totalPage;
		$data['type'] = $type;
		$data['status'] = $status;
		$data['startdate'] = $startdate;
		$data['enddate'] = $enddate;
		$data['limit'] = $limit;
		
		$this->_loadHeader();
		$this->_loadMenu();
		$this->load->view($this->_template_f . '/budget/user', $data);
		$this->_loadFooter();
	}
	
	function paidView()
	{
		$key = trim($this->input->get('key',true));
		$uid = (int)$this->input->get('uid',true);
		
		$data = array();
		$data['lang'] = $this->lang->line('budget');
		$data['info'] = $this->budget_model->get_payment_log_info($uid, $key);
		$data['logs'] = $this->budget_model->get_paid_log_detail($uid, $data['info']->paymentdate);
		$data['user'] = $this->budget_model->get_user_info($uid);
		$data['dayView'] = new DateTime($data['info']->payment_time);
		$data['prevDay'] = new DateTime($data['info']->payment_time);
		$data['prevDay'] = $data['prevDay']->sub(new DateInterval('P1D'));
		if($data['user'] == null || count($data['user']) == 0)
		{
			redirect(site_url('budget')); die();
		}
		$this->_loadHeader();
		$this->_loadMenu();
		$this->load->view($this->_template_f . '/budget/paid_view', $data);
		$this->_loadFooter();
	}
	
	function transactionMiss()
	{
		$page = (int)$this->input->get('page', true);
		if($page < 1) $page = 1;
		
		$logs = $this->budget_model->get_payment_tranmiss($page, 20);
		
		$data = array();
		$data['items'] = $logs['items'];
		$data['lang'] = $this->lang->line('budget');
		$data['page'] = $page;
		$data['totalPage'] = $logs['pages'];
		$data['check'] = (int)$this->input->get('check',true);
		
		$this->_loadHeader();
		$this->_loadMenu();
		$this->load->view($this->_template_f . '/budget/transactions_miss_list', $data);
		$this->_loadFooter();
	}
	
	function check_soha()
	{
		$order_code = trim($this->input->get('code'));
		$info = $this->budget_model->payment_log_info_by_key($order_code);
		//Xac dinh kieu order bang the atm hay the cao dien thoai
		$queryType = 'query';
		if(strpos($info->typepay, ' - Card') !== false || strpos($info->typepay, ' - Mobile card') !== false)
		{
			$queryType = 'queryMobileCard';
		}
		
		$arrReturn = array();
		$this->load->library('pg_checkout');
		if($order_code != '')
		{
			$arrReturn = $this->pg_checkout->queryOrderStatus($order_code, $queryType);
		}

		if(count($arrReturn) > 0 && ((int)$arrReturn['shp_order_status']) == 1) echo 1;
		else echo 0;
	}
	
	function _loadHeader($title = '')
	{
		$header = array();
		$header['title'] = $title == '' ? 'Self serving' : $title;
		$header['template_f'] = $this->_template_f;
		
		$header['username'] = '';
		$header['userid'] = '';
		$header['permitid'] = '';
		$header['module_name'] = 'budget';
		$header['lang'] = $this->_lang;
		$header['common_lang'] = $this->lang->line('common_lang');
        
		if($this->session->userdata('uname'))
		{
			$header['username'] = $this->session->userdata('uname');
			$header['userid'] = $this->session->userdata('uid');
			$header['permitid'] = $this->session->userdata('permitid');
		}
		
		 //load header
		$this->load->view($this->_template_f . 'preheader_view');
		$this->load->view($this->_template_f . 'header_view', $header);
	}
	
	function _loadMenu()
	{
		$data = array();
		$data['username'] = '';
		$data['userid'] = '';
		$data['permitid'] = '';
		$data['lang'] = $this->_lang;
		if($this->session->userdata('uname'))
		{
			$data['username'] = $this->session->userdata('uname');
			$data['userid'] = $this->session->userdata('uid');
			$data['permitid'] = $this->session->userdata('permitid');
		}
		$data['common_lang'] = $this->lang->line('common_lang');
        
		$this->load->view($this->_template_f . 'menu_view',$data);
	}
	
	function _loadFooter()
	{
		$this->load->view($this->_template_f . 'footer_view');
	}
}